# read in data
library(stringr)
library(data.table)
library(ggplot2)
library(dplyr)
library(lubridate)
library(lfe)
library(sjPlot)
library(stargazer)
library(standardize)
library(broom)
library(fixest)

###################### set your wd
setwd()
################# Load Tweets and FB

congressFBlocal <- readRDS("~Data/congressfb_aggregated_RR.rds")
congresstweetslocal <- readRDS("~Data/congresstweets_aggregated_RR.rds")

#######

###### campaign finance
DailyDonations2010 <- readRDS("~Data/DonationsData/DailyDonations2010.rds")
DailyDonations2012 <- readRDS("~Data/DonationsData/DailyDonations2012.rds")
DailyDonations2014 <- readRDS("~Data/DonationsData/DailyDonations2014.rds")
DailyDonations2016 <- readRDS("~Data/DonationsData/DailyDonations2016.rds")
DailyDonations2018 <- readRDS("~Data/DonationsData/DailyDonations2018.rds")
DailyDonations2020 <- readRDS("~Data/DonationsData/DailyDonations2020.rds")
DailyDonations2022 <- readRDS("~Data/DonationsData/DailyDonations2022.rds")


DailyDonations2010 <- filter(DailyDonations2010, date < as.Date("2010-11-03"))
DailyDonations2012 <- filter(DailyDonations2012, date < as.Date("2012-11-07"))
DailyDonations2014 <- filter(DailyDonations2014, date < as.Date("2014-11-05"))
DailyDonations2016 <- filter(DailyDonations2016, date < as.Date("2016-11-09"))
DailyDonations2018 <- filter(DailyDonations2018, date < as.Date("2018-11-07"))
DailyDonations2020 <- filter(DailyDonations2020, date < as.Date("2020-11-04"))
DailyDonations2022 <- filter(DailyDonations2022, date < as.Date("2022-11-08"))


donations2010year <- DailyDonations2010 %>%
  group_by(cmte_id) %>%
  dplyr::summarize(
    donations = sum(donations),
    total = sum(total),
    instatenum = sum(instatenum),
    indnum = sum(indnum)
  ) %>%
  ungroup()

donations2012year <- DailyDonations2012 %>%
  group_by(cmte_id) %>%
  dplyr::summarize(
    donations = sum(donations),
    total = sum(total),
    instatenum = sum(instatenum),
    indnum = sum(indnum)
  ) %>%
  ungroup()

donations2014year <- DailyDonations2014 %>%
  group_by(cmte_id) %>%
  dplyr::summarize(
    donations = sum(donations),
    total = sum(total),
    instatenum = sum(instatenum),
    indnum = sum(indnum)
  ) %>%
  ungroup()

donations2016year <- DailyDonations2016 %>%
  group_by(cmte_id) %>%
  dplyr::summarize(
    donations = sum(donations),
    total = sum(total),
    instatenum = sum(instatenum),
    indnum = sum(indnum)
  ) %>%
  ungroup()

donations2018year <- DailyDonations2018 %>%
  group_by(cmte_id) %>%
  dplyr::summarize(
    donations = sum(donations),
    total = sum(total),
    instatenum = sum(instatenum),
    indnum = sum(indnum)
  ) %>%
  ungroup()

donations2020year <- DailyDonations2020 %>%
  group_by(cmte_id) %>%
  dplyr::summarize(
    donations = sum(donations),
    total = sum(total),
    instatenum = sum(instatenum),
    indnum = sum(indnum)
  ) %>%
  ungroup()

donations2022year <- DailyDonations2022 %>%
  group_by(cmte_id) %>%
  dplyr::summarize(
    donations = sum(donations),
    total = sum(total),
    instatenum = sum(instatenum),
    indnum = sum(indnum)
  ) %>%
  ungroup()

donations2010year$instatepct <- (donations2010year$instatenum / donations2010year$donations) *100
donations2010year$indpct <- (donations2010year$indnum / donations2010year$donations) *100
donations2012year$instatepct <- (donations2012year$instatenum / donations2012year$donations) *100
donations2012year$indpct <- (donations2012year$indnum / donations2012year$donations) *100
donations2014year$instatepct <- (donations2014year$instatenum / donations2014year$donations) *100
donations2014year$indpct <- (donations2014year$indnum / donations2014year$donations) *100
donations2016year$instatepct <- (donations2016year$instatenum / donations2016year$donations) *100
donations2016year$indpct <- (donations2016year$indnum / donations2016year$donations) *100
donations2018year$instatepct <- (donations2018year$instatenum / donations2018year$donations) *100
donations2018year$indpct <- (donations2018year$indnum / donations2018year$donations) *100
donations2020year$instatepct <- (donations2020year$instatenum / donations2020year$donations) *100
donations2020year$indpct <- (donations2020year$indnum / donations2020year$donations) *100
donations2022year$instatepct <- (donations2022year$instatenum / donations2022year$donations) *100
donations2022year$indpct <- (donations2022year$indnum / donations2022year$donations) *100

donations2010year$congress <- 111
donations2012year$congress <- 112
donations2014year$congress <- 113
donations2016year$congress <- 114
donations2018year$congress <- 115
donations2020year$congress <- 116
donations2022year$congress <- 117

donations <- rbind(donations2010year, donations2012year, donations2014year, donations2016year, donations2018year, donations2020year, donations2022year)

####################

congresstweetslocal$feccongress <- paste0(as.character(congresstweetslocal$FECcommittee),"-", as.character(congresstweetslocal$congress.x))
congressFBlocal$feccongress <- paste0(as.character(congressFBlocal$FECcommittee),"-", as.character(congressFBlocal$congress.x))
donations$feccongress <- paste0(as.character(donations$cmte_id),"-", as.character(donations$congress))

congresstweetsdonations <- left_join(congresstweetslocal, donations, by = "feccongress")
congressFBdonations <- left_join(congressFBlocal, donations, by = "feccongress")

congresstweetsdonations$competitive <- ifelse(congresstweetsdonations$PVIABS < 10, 1, 0)
congresstweetsdonations$congressparty <- paste0(as.character(congresstweetsdonations$congress.x),"-", as.character(congresstweetsdonations$party), "-", as.character(congresstweetsdonations$competitive))
congresstweetsdonations$congressparty <- paste0(as.character(congresstweetsdonations$congress.x),"-", as.character(congresstweetsdonations$party))

congressFBdonations$competitive <- ifelse(congressFBdonations$PVIABS < 10, 1, 0)
congressFBdonations$congressparty <- paste0(as.character(congressFBdonations$congress.x),"-", as.character(congressFBdonations$party), "-", as.character(congressFBdonations$competitive))
congressFBdonations$congressparty <- paste0(as.character(congressFBdonations$congress.x),"-", as.character(congressFBdonations$party))

#####################

congresstweetsdonations <- congresstweetsdonations %>% filter(!is.na(Vote_Pct_2PT))
congressFBdonations <- congressFBdonations %>% filter(!is.na(Vote_Pct_2PT))

tweetsinstate <- felm(scale(instatepct) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + Cmte_Chair + Qual_Challenger| congressparty + icpsr.y, data = congresstweetsdonations)
FBinstate <- felm(scale(instatepct) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + Cmte_Chair + Qual_Challenger| congressparty + icpsr.y, data = congressFBdonations)
tweetsdonations <- felm(scale(donations) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + Cmte_Chair + Qual_Challenger| congressparty + icpsr.y, data = congresstweetsdonations)
FBdonations <- felm(scale(donations) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + Cmte_Chair + Qual_Challenger| congressparty + icpsr.y, data = congressFBdonations)
tweetstotal <- felm(scale(total) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + Cmte_Chair + Qual_Challenger| congressparty + icpsr.y, data = congresstweetsdonations)
FBtotal <- felm(scale(total) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + Cmte_Chair + Qual_Challenger| congressparty + icpsr.y, data = congressFBdonations)

tab_model(tweetsinstate, FBinstate,tweetsdonations, FBdonations,tweetstotal, FBtotal)
stargazer(tweetsinstate, FBinstate,tweetsdonations, FBdonations,tweetstotal, star.cutoffs = c(.05, .01, .001), type = "html", out = "localdonationsFE.htm")


tweetsinstate <- felm(scale(instatepct) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger| congressparty + state, data = congresstweetsdonations)
FBinstate <- felm(scale(instatepct) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger| congressparty + state, data = congressFBdonations)
tweetsdonations <- felm(scale(donations) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger| congressparty + state, data = congresstweetsdonations)
FBdonations <- felm(scale(donations) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger| congressparty + state, data = congressFBdonations)
tweetstotal <- felm(scale(total) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger| congressparty + state, data = congresstweetsdonations)
FBtotal <- felm(scale(total) ~ scale(local) + NPDistScale + Chamber.Majority + PVIAbsscale + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger| congressparty + state, data = congressFBdonations)

tab_model(tweetsinstate, FBinstate,tweetsdonations, FBdonations,tweetstotal, FBtotal)
stargazer(tweetsinstate, FBinstate,tweetsdonations, FBdonations,tweetstotal, star.cutoffs = c(.05, .01, .001), type = "html", out = "localdonationsPooled.htm")

##############################
###interaction

tweetsinstate <- lm(scale(instatepct) ~ local + NPDistScale + Chamber.Majority + PVIABS + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger + local*PVIABS + congressparty + state, data = congresstweetsdonations)
FBinstate <- lm(scale(instatepct) ~ local + NPDistScale + Chamber.Majority + PVIABS + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger + local*PVIABS + congressparty + state, data = congressFBdonations)
tweetsdonations <- lm(scale(donations) ~ local + NPDistScale + Chamber.Majority + PVIABS + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger + local*PVIABS + congressparty + state, data = congresstweetsdonations)
FBdonations <- lm(scale(donations) ~ local + NPDistScale + Chamber.Majority + PVIABS + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger + local*PVIABS + congressparty + state, data = congressFBdonations)
tweetstotal <- lm(scale(total) ~ local + NPDistScale + Chamber.Majority + PVIABS + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger + local*PVIABS + congressparty + state, data = congresstweetsdonations)
FBtotal <- lm(scale(total) ~ local + NPDistScale + Chamber.Majority + PVIABS + Freshman + Seniority + chamber.y + female + Cmte_Chair + Qual_Challenger + local*PVIABS + congressparty + state, data = congressFBdonations)

P1 <- plot_model(tweetsinstate, type = "pred", terms = c("local", "PVIABS [0, 50]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Pct. In-State Donations (Scaled)", 
    color = "Absolute PVI Value",  # Change legend title
    title = "In-State Donations (Twitter)"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

P2 <- plot_model(FBinstate, type = "pred", terms = c("local", "PVIABS [0, 50]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Pct. In-State Donations (Scaled)", 
    color = "Absolute PVI Value",  # Change legend title
    title = "In-State Donations (Facebook)"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

P3 <-plot_model(tweetsdonations, type = "pred", terms = c("local", "PVIABS [0, 50]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Total Donations (Scaled)", 
    color = "Absolute PVI Value",  # Change legend title
    title = "Total Donations (Twitter)"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

P4 <- plot_model(FBdonations, type = "pred", terms = c("local", "PVIABS [0, 50]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Total Donations (Scaled)", 
    color = "Absolute PVI Value",  # Change legend title
    title = "Total Donations (Facebook)"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

P5 <- plot_model(tweetstotal, type = "pred", terms = c("local", "PVIABS [0, 50]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Total Fundraising (Scaled)", 
    color = "Absolute PVI Value",  # Change legend title
    title = "Total Fundraising (Twitter)"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

P6 <- plot_model(FBtotal, type = "pred", terms = c("local", "PVIABS [0, 50]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Total Fundraising (Scaled)", 
    color = "Absolute PVI Value",  # Change legend title
    title = "Total Fundraising (Facebook)"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

library(gridExtra)
grid.arrange(P1, P2, P3, P4, P5, P6, ncol = 2, nrow = 3)
ggsave("Donations_Interactions_appendix.png")

tab_model(tweetsinstate, FBinstate,tweetsdonations, FBdonations,tweetstotal, FBtotal)
stargazer(tweetsinstate, FBinstate,tweetsdonations, FBdonations,tweetstotal, FBtotal, star.cutoffs = c(.05, .01, .001), type = "html", out = "localdonationsinteraction.htm")


#####################

coefs <- read.csv("~Data//LocalElectionCoefs.csv")

donations <- filter(coefs, Question == "In-State" | Question == "Total" | Question == "Donations")

ggplot(donations, aes(x=Question, y=coef, group=Measure, color=Measure)) + 
  geom_point(position = position_dodge(.3), size = 2.5) +
  geom_errorbar(aes(ymin = lower, ymax = upper),
                position = position_dodge(.3),
                width = 0) +
  geom_hline(yintercept=0, linetype="dashed", color = "grey50")+
  ylab("Difference Based on Localized Message Pct.")+
  scale_colour_manual(values = c("grey70", "grey40"))+
  theme_bw() +
  facet_wrap(~Model)+
  theme(
    legend.position = "bottom",
    axis.title.x = element_blank())
ggsave("Donations Local.png")
